package com.mxchip.livestar.ui.contact

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.mxchip.livestar.R
import com.mxchip.livestar.base.ContactPOJO
import com.mxchip.livestar.ui.ItemHolder
import kotlinx.android.synthetic.main.item_address_book.*

class AddressBookAdapter(private val onClickListener: ((View, ContactPOJO) -> Unit)? = null) : RecyclerView.Adapter<ItemHolder>() {

    var data: List<ContactPOJO> = emptyList()

    override fun getItemCount(): Int {
        return data.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHolder {
        val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.item_address_book, parent, false)
        return ItemHolder(view)
    }

    override fun onBindViewHolder(holder: ItemHolder, position: Int) {
        val (_, contactName, contactNumber) = data[position]
        //绘制联系人名称
        holder.name.text = contactName
        holder.number.text =  contactNumber

        holder.containerView.setOnClickListener {
            onClickListener?.invoke(it, data[position])
        }
    }
}